package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.Color;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Delete extends JInternalFrame {
	Connection conn;
	private JLabel 시리얼;
	private JLabel label;
	private JLabel label_1;
	private JTextField deleteserial;
	private JTextField deletecode;
	private JTextField deleteea;
	private JButton btnNewButton;
	private JButton deletebtn;
	private JLabel deletestatus;

	public Delete(Connection conn) {
		super("데이타 삭제", true, true, true, true);
		getContentPane().setBackground(new Color(153, 255, 204));
		this.conn = conn;
		setTitle("데이타 삭제");
		setBounds(562, 0, 311, 150);
		getContentPane().setLayout(null);
		getContentPane().add(get시리얼());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getDeleteserial());
		getContentPane().add(getDeletecode());
		getContentPane().add(getDeleteea());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getDeletebtn());
		getContentPane().add(getDeletestatus());

	}

	private JLabel get시리얼() {
		if (시리얼 == null) {
			시리얼 = new JLabel("시리얼 :");
			시리얼.setForeground(new Color(102, 0, 255));
			시리얼.setFont(new Font("굴림", Font.BOLD, 15));
			시리얼.setBounds(10, 10, 70, 20);
		}
		return 시리얼;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드 :");
			label.setForeground(new Color(102, 0, 255));
			label.setFont(new Font("굴림", Font.BOLD, 15));
			label.setBounds(10, 36, 70, 20);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("수량 :");
			label_1.setForeground(new Color(102, 0, 255));
			label_1.setFont(new Font("굴림", Font.BOLD, 15));
			label_1.setBounds(10, 62, 70, 20);
		}
		return label_1;
	}
	private JTextField getDeleteserial() {
		if (deleteserial == null) {
			deleteserial = new JTextField();
			deleteserial.setBounds(77, 10, 116, 21);
			deleteserial.setColumns(10);
		}
		return deleteserial;
	}
	private JTextField getDeletecode() {
		if (deletecode == null) {
			deletecode = new JTextField();
			deletecode.setDisabledTextColor(Color.BLACK);
			deletecode.setEditable(false);
			deletecode.setEnabled(false);
			deletecode.setColumns(10);
			deletecode.setBounds(77, 36, 116, 21);
		}
		return deletecode;
	}
	private JTextField getDeleteea() {
		if (deleteea == null) {
			deleteea = new JTextField();
			deleteea.setDisabledTextColor(Color.BLACK);
			deleteea.setEditable(false);
			deleteea.setEnabled(false);
			deleteea.setColumns(10);
			deleteea.setBounds(77, 62, 116, 21);
		}
		return deleteea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.setBackground(new Color(204, 255, 51));
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						if(deleteserial.getText().equals("")){
							deletestatus.setText("시리얼번호를 입력하세요.");
							deletecode.setText("");
							deleteea.setText("");
							deletebtn.setVisible(false);
						}
						else{
							int s = Integer.parseInt(deleteserial.getText());
							String sql = "select * from pro where serial = ?";
							PreparedStatement pstmt = conn.prepareStatement(sql);
							pstmt.setInt(1, s);
							
							ResultSet rs = pstmt.executeQuery();
							if(rs.next()){
								deletecode.setText(rs.getString("code"));
								deleteea.setText("" + rs.getInt("ea"));
								deletestatus.setText("조회 완료!!!");
								deleteserial.requestFocus();
								deletebtn.setVisible(true);
							}
							else{
								deletestatus.setText("일치하는 데이터가 없습니다.");
								deletecode.setText("");
								deleteea.setText("");
								deleteserial.requestFocus();
								deletebtn.setVisible(false);
							}
							
							rs.close();
							pstmt.close();
						}
						
								
					}catch(Exception ex){
						ex.printStackTrace();
						deletestatus.setText("형식에 맞게 입력하세요.");
						deletebtn.setVisible(false);
					}
				
				}
			});
			btnNewButton.setForeground(new Color(255, 51, 0));
			btnNewButton.setFont(new Font("굴림", Font.BOLD, 15));
			btnNewButton.setBounds(205, 9, 82, 21);
		}
		return btnNewButton;
	}
	private JButton getDeletebtn() {
		if (deletebtn == null) {
			deletebtn = new JButton("삭제");
			deletebtn.setVisible(false);
			deletebtn.setBackground(new Color(204, 255, 51));
			deletebtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						if(deleteserial.getText().equals("")){
							deletestatus.setText("삭제할 시리얼 번호를 입력하세요.");
						}
						else{
							int s = Integer.parseInt(deleteserial.getText());
							String sql = "delete from pro where serial = ?";
							PreparedStatement pstmt = conn.prepareStatement(sql);
							pstmt.setInt(1, s);
							
							pstmt.executeUpdate();
							pstmt.close();
							deletestatus.setText("데이터가 삭제 되었습니다.");
							deleteserial.setText("");
							deletecode.setText("");
							deleteea.setText("");
							deleteserial.requestFocus();
							deletebtn.setVisible(false);
						}
					}catch(Exception ex){
						ex.printStackTrace();
						deletestatus.setText("형식에 맞게 입력하세요.");
						deletebtn.setVisible(false);
					}
					
				
				}
			});
			deletebtn.setForeground(new Color(255, 51, 0));
			deletebtn.setFont(new Font("굴림", Font.BOLD, 15));
			deletebtn.setBounds(205, 61, 82, 21);
		}
		return deletebtn;
	}
	private JLabel getDeletestatus() {
		if (deletestatus == null) {
			deletestatus = new JLabel("");
			deletestatus.setFont(new Font("굴림", Font.BOLD, 15));
			deletestatus.setHorizontalAlignment(SwingConstants.CENTER);
			deletestatus.setBounds(10, 88, 277, 20);
		}
		return deletestatus;
	}
}
